import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class JsonTool {
    public static void main(String[] args) {
        File file = new File("E:\\storage\\json\\tash.json");
        String str = getStr(file);
//        System.out.println(str.length());
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray allDateArr = jsonObject.getJSONArray("all");

        List<String> outLabels = new ArrayList<>();
        List<String> miLabels = new ArrayList<>();
        List<String> inLabels = new ArrayList<>();


        for (int i = 0; i < allDateArr.size(); i++) {
            JSONObject outJson = (JSONObject) allDateArr.get(i);
            String outLabel = (String) outJson.get("label");
//            outLabels.add(outLabel);

            //第二层循环
            if (outJson.containsKey("children")) {
                JSONArray midArr = outJson.getJSONArray("children");
                for (Object mid : midArr) {
                    JSONObject midJson = (JSONObject) mid;
                    String midLabel = (String) midJson.get("label");
//                    System.out.println(outLabel + " --> " + midLabel);
//              第三层循环
                    JSONArray inArr = midJson.getJSONArray("children");
                    if (midJson.containsKey("children") && inArr != null) {
                        for (Object inJson : inArr) {
                            JSONObject inLabel = (JSONObject) inJson;
                            String label = (String) inLabel.get("label");
                            inLabels.add(label);
                            outLabels.add(outLabel);
                            miLabels.add(midLabel);
//                            System.out.println(outLabel + " --> " + midLabel + " --> " + label);
                        }
                    } else {
                        outLabels.add(outLabel);
                        miLabels.add(midLabel);
                        inLabels.add("null");
//                        System.out.println(outLabel + " --> " + midLabel + " --> null");
                    }
                }
            } else {
                outLabels.add(outLabel);
                miLabels.add("null");
                inLabels.add("null");
//                System.out.println(outLabel + " --> null " + " --> null");
            }
        }

        for (int i = 0; i < inLabels.size(); i++) {
            System.out.printf("%-15s||%-12s||%-20s\n",outLabels.get(i),miLabels.get(i),inLabels.get(i));
        }

    }

    private static void dealPartJson() {
        File file = new File("E:\\storage\\json\\part.json");
        String str = getStr(file);
//        System.out.println(str);
        JSONObject jsonObject = JSONObject.parseObject(str);
        System.out.println(jsonObject.getClass());
        JSONArray children = jsonObject.getJSONArray("children");

        ArrayList<String> labels = new ArrayList<>();

        for (int i = 0; i < children.size(); i++) {
//            System.out.println(children.get(i));
            JSONObject json = (JSONObject) children.get(i);
            String label = (String) json.get("label");
            labels.add(label);
        }
        System.out.println(labels);
    }

    //把一个文件中的内容读取成一个String字符串
    public static String getStr(File jsonFile){
        String jsonStr = "";
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
